#include "turtle.h"
#define TURTLE_SPEED 0.05f;

CTurtle::CTurtle() : CMovable()
{

}

CTurtle::~CTurtle()
{

}

CTurtle::CTurtle(int way,float X, float Y)
{
	state = 0;
	reality = 21;
	v_y = 0;
	movingWay = way;
	v_x = TURTLE_SPEED;
	if(way == 0) v_x = - TURTLE_SPEED;
	pos_x = X;
	pos_y = Y;
	width = 16;
	height = 24;
}

void CTurtle::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, int t)
{
	DWORD now = GetTickCount();
	if(state < 2)
		Update(t);
	else
	{
		if(state == 2 && now - last_time > 3000)
		{
			timeGetUp = now;
			state = 3;
		}
		else if(state == 3)
		{
			if(now - timeGetUp > 2000)
			{
				state = 0;
				height = 24;
				v_x = TURTLE_SPEED;
				if(movingWay == 0) v_x = - TURTLE_SPEED;
			}
		}
	}
	if ((state == 0 || state == 3) && now - last_time > 200)
	{
		anim=(anim+1) % 2;
		last_time = now;
	}
	SpriteHandlerEP->Begin(D3DXSPRITE_ALPHABLEND);
	if(state == 0)
		EnemiesPic->Render(_BackBuffer,pos_x,pos_y,width,height,0,5+anim+movingWay*2);
	else if(state >= 1)
		EnemiesPic->Render(_BackBuffer,pos_x,pos_y,width,height,0,12-anim);
	SpriteHandlerEP->End();
}

void CTurtle::Update(int t)
{
	int ii = BeforeMove();
////////////////////////////////////////////////////////////
	pos_x += v_x*t;
	pos_y += v_y*t;
////////////////////////////////////////////////////////////
	cTree->Check(pos_x,pos_y+2,width,1);
	if(cTree->lastRes == 0 && v_y < 0.5f)
		v_y += 0.1f;
	else if(cTree->lastRes > 0)
	{
		if(ArrayObj[cTree->res[0]]->reality < 3)
		{
			v_y = 0;
			pos_y = ArrayObj[cTree->res[0]]->pos_y - ArrayObj[cTree->res[0]]->height;
		}
	}

	cTree->Check(pos_x+width-1,pos_y-height+5,1,height-10);
	if(cTree->lastRes > 0)
	{
		if(ArrayObj[cTree->res[0]]->reality < 10 || (ArrayObj[cTree->res[0]]->reality >= 20 && state != 1))
		{
			pos_x = ArrayObj[cTree->res[0]]->pos_x - width;
			v_x = -v_x;
			movingWay = 0;
		}
		else
			ArrayObj[cTree->res[0]]->Die();
	}

	cTree->Check(pos_x+1,pos_y-height+5,1,height-10);
	if(cTree->lastRes > 0)
	{
		if(ArrayObj[cTree->res[0]]->reality < 10 || (ArrayObj[cTree->res[0]]->reality >= 20 && state != 1))
		{
			pos_x = ArrayObj[cTree->res[0]]->pos_x + ArrayObj[cTree->res[0]]->width;
			v_x = -v_x;
			movingWay = 1;
		}
		else if(state == 1)
			ArrayObj[cTree->res[0]]->Die();
	}

	cTree->Add(this,ii);
	mTree->AddPixel(mTree->Root,ii,pos_x,pos_y);
}

void CTurtle::Slice(int way)
{
		state = 1;
		anim = 0;
		movingWay = way;
		v_x = 10*TURTLE_SPEED;
		if(movingWay == 0) v_x = -v_x;
}

void CTurtle::isJumpedOn(CObjects * obj)
{
	if(obj->havingStar == 1)
	{
		Die();
		return;
	}
	if(state < 2)
	{
		anim = 0;
		state = 2;
		v_x = 0;
		int i = cTree->Del(pos_x,pos_y,width,height);
		height = 20;
		cTree->Add(this,i);
		last_time = GetTickCount();
	}
	else
	{
		Slice(1);
	}
}

void CTurtle::Touch(CObjects *obj)
{
	if(obj->havingStar == 1)
	{
		Die();
		return;
	}
	if(state == 0)
	{
		obj->lvDown();
		movingWay = 1 - movingWay;
		v_x = - v_x;
	}
	else if(state == 1)
	{
		obj->lvDown();
	}
	else
	{
		Slice(obj->movingWay);
	}
}

void CTurtle::Die()
{
	life = 0;
	int i = cTree->Del(pos_x,pos_y,width,height);
	i = mTree->Del(pos_x,pos_y,1,1);
}